use std::collections::HashMap;
use std::time;

#[test]
fn vec_test() {
    let v = vec![1, 3, 5];

    for i in v {
        println!("i {:?}", i);
    }

    // error[E0382]: borrow of moved value: `v`
    // println!("v {:?}", v);
}

#[test]
fn vec_iter_test() {
    let v = vec![1, 3, 5];

    for i in v.into_iter() {
        println!("i {:?}", i);
    }

    // error[E0382]: borrow of moved value: `v`
    // println!("v {:?}", v);
}

fn push_bench(v: &mut Vec<i32>, total: i32) {
    let start = time::SystemTime::now();
    for i in 1..total {
        v.push(i)
    }
    let end = time::SystemTime::now();
    println!("cost =  {:?}", end.duration_since(start).unwrap());
}

#[test]
fn push_test() {
    let total = 1_000_000_0;
    let mut v = Vec::new();
    push_bench(&mut v, total);

    let mut v = Vec::new();

    v.reserve(total as usize);
    push_bench(&mut v, total);
}

#[test]
fn iter_collect_test() {
    let v: Vec<i32> = (1..10).collect();

    println!("v {:?}", v);

    let s = (1..10).fold(1u64, |mul, x| mul * x);

    println!("s {:?}", s);
}

#[test]
fn iter_consumer_test() {
    let names = vec!["WaySLOG", "Mike", "Elton"];
    let scores = vec![60, 80, 100];
    let score_map: HashMap<_, _> = names.iter().zip(scores.iter()).collect();
    println!("{:?}", score_map);
}

#[test]
fn iter_narcissistic_test() {
    let nar = |x: &i32| -> bool {
        let mut t = *x;
        let mut v = vec![];
        while t > 0 {
            let n = t % 10;
            v.push(n);
            t /= 10;
        }

        let mut sum = 0;
        let len = v.len();
        for i in v {
            sum += i.pow(len as u32);
        }
        // let i = x % 10;
        // let j = x / 10 % 10;
        // let m = x / 100 % 10;
        // println!("i = {:?}, j = {:?}, m = {}", i, j.pow(2), m);
        // let y = i.pow(3) + j.pow(3) + m.pow(3);
        return *x == sum;
    };
    // println!("narcissistic {:?}", nar(153));

    let v = (100..).filter(nar).take(10).collect::<Vec<i32>>();

    println!("first 10 shuixianshu is  {:?}", v);
}
